<template>
  <span>
    <!-- {{val}}  此组件用于百分比-->
    <input
      v-model.trim="val"
      :disabled="disabled"
      :class="$style.inputNumber"
      class="ant-input-number-input input_number"
      :min="min"
      max="99.99"
      type="number"
      @mousewheel.prevent="wheelFn($event)"
      @blur="sendValue($event)"
      @input="inputNum($event)"
    />
  </span>
</template>
<script>
export default {
  props: {
    min: {
      type: Number,
      default: 0
    },
    value: {
      type: [String, Number],
      default: 0
    },
    disabled: {
      type: Boolean,
      default: false
    },
    precision: {
      type: Number,
      default: 2
    }
  },
  data() {
    return {
      val: (this.value * 100 || 0).toFixed(2)
    }
  },
  watch: {
    value(n, o) {
      this.val = (this.value * 100 || 0).toFixed(2)
    }
  },
  methods: {
    sendValue(e) {
      this.val = (this.val * 1).toFixed(this.precision)
      if (e.target.valueAsNumber < this.min) {
        this.val = this.min
      }
      if (isNaN(e.target.valueAsNumber)) {
        this.val = this.min
      }
      // this.$emit('input', this.val)
    },
    wheelFn(e) {
      const evt = e || window.event
      if (evt.preventDefault) {
        // Firefox
        evt.preventDefault()
        evt.stopPropagation()
      } else {
        // IE
        evt.cancelBubble = true
        evt.returnValue = false
      }
      return false
    },
    inputNum(e) {
      this.val = e.target.valueAsNumber > 99.99 ? 0 : this.val
      console.log(this.value) // 2099
      this.$emit('input', (this.val / 100).toFixed(4))
    }
  }
}
</script>
<style lang="less" module>
.inputNumber {
  border: 1px solid #e2dfdf !important;
  background: #fff !important;
}
.input_number:focus {
  border: 1px solid #1989fa !important;
}
</style>
